<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
import {dotData} from '@/utils'
import StompJs from '@/utils/StompJs'
import Easemob from '@/utils/Easemob'
import {getLoginUser} from '@/utils/ajax'
import {mapActions} from 'vuex'
import {isLogin} from '@/utils/auth'
export default{
  name: 'App',
  created() {
    // 请求菜单数据 这里一般放入项目初始化需要的整体配置只会执行一次
    // 请求菜单数据
    this.$store.dispatch('INIT_MENU_LIST')
    // 接收消息推送
    this.getMessage()
    // 初始全局配置
    this.initConfig()
    // 初始im
    this.initIm()
  },
  methods: {
    getMessage() {
      // 充值消息
      if (['fhbMant'].indexOf(process.env.projectType) >= 0) {
        let s1 = new StompJs()
        let userId = getLoginUser('id')
        s1.getClient().then(_ => {
          s1.subscribe('/topic/' + userId + 'recharge', (message) => {
            let information = JSON.parse(message.body)
            let newMoney = dotData(information, 'money')
            // 告知充值页面recharge.vue 成功充值了，改变全局状态为false-->关闭‘温馨提示’弹框
            this.setRechargeResult({key: 'wallet', result: false})
            this.$confirm(`
            <p>已成功充值 <span class="font-red">${newMoney}</span> 元</p>
            <p>是否进入钱包查看优惠券?</p>
          `, '提示', {
              type: 'success',
              center: true,
              confirmButtonText: '确定',
              showCancelButton: true,
              cancelButtonText: '取消',
              dangerouslyUseHTMLString: true
              // closeOnClickModal: false,
            }).then(_ => {
              this.$router.push({path: '/redirect/ss-fhb/myWallet/index'})
            })
          })

          // 系统提示 催款 退款 异常 优惠券coupons
          s1.subscribe('/topic/' + userId, (message) => {
            let information = JSON.parse(message.body)
            this.$notify({
              duration: 0,
              position: 'bottom-right',
              dangerouslyUseHTMLString: true,
              title: information.msgContent.title,
              message: information.msgContent.content
            })
          })
          // web公告
          s1.subscribe('/topic/sys.topic.handle.order.WEBFhbAnnouncement', (message) => {
            let information = JSON.parse(message.body)
            this.$notify({
              duration: 0,
              position: 'bottom-right',
              dangerouslyUseHTMLString: true,
              title: information.msgContent.title,
              message: information.msgContent.synopsis
            })
          })
        })
      }
    },
    initConfig() {
      if (!isLogin()) {
        return
      }
      if (['fhbMant'].indexOf(process.env.projectType) >= 0) {
        // 每次刷新页面时就请求配置消息，根据后台的相关配置来决定web商家端是否展示相关信息
        // 读取底层配置之参考报价配置referenceOffer---‘是否显示参考报价fhbwebShowRO’：
        // 读取底层配置之参支付提现配置payChannels---‘是否显示各页面的 支付按钮：
        let moduleTypeList = ['referenceOffer', 'payChannels']
        let typeListToString = moduleTypeList.join(',')
        this.$ajax.get('/ms-fahuobao-order/control/get-control-list', {moduleType: typeListToString}).then(response => {
          if (response.success && Array.isArray(response.data)) {
            response.data.forEach(v => {
              // 当配置接口返回的是否显示web参考报价为真：给全局变量赋新值为 true
              if (v.name === 'fhbwebShowRO' && v.value === 'true') { // web是否显示参考报价（标识：fhbwebShowRO）
                this.SET_GLOBAL_CONFIG({key: 'fhbwebShowRO', value: true})
              }
              if (v.name === 'withdrawalPC' && v.value === 'true') { // 余额提现 是否禁用（标识：withdrawalPC）
                this.SET_GLOBAL_CONFIG({key: 'withdrawalPC', value: true})
              }
              if (v.name === 'aliPayPC' && v.value === 'true') { // 支付宝付款 是否禁用（标识：aliPayPC）
                this.SET_GLOBAL_CONFIG({key: 'aliPayPC', value: true})
              }
              if (v.name === 'weChatPayPC' && v.value === 'true') { // 微信付款 是否禁用（标识：weChatPayPC）
                this.SET_GLOBAL_CONFIG({key: 'weChatPayPC', value: true})
              }
              if (v.name === 'walletPC' && v.value === 'true') { // 余额支付 是否禁用（标识：walletPC）
                this.SET_GLOBAL_CONFIG({key: 'walletPC', value: true})
              }
            })
          }
        })
      }
    },
    initIm() {
      // im 登陆
      if (isLogin() && this.$route.path !== '/login' && ['fhbMant'].indexOf(process.env.projectType) >= 0) {
        const im = new Easemob()
        this.$ajax.get('/ms-message-im/easemob/query-id').then(response => {
          if (response.success) {
            const data = response.data || {}
            if (data) {
              im.login(data.easemobName, data.easemobPass)
            }
          }
        })
      }
    },
    ...mapActions([
      'setRechargeResult',
      'SET_GLOBAL_CONFIG'
    ])
  }
}
</script>
